package com.cheung.leetcode.binarysearch;

public class LC367 {


    public static boolean isPerfectSquare(int num) {
        if (num == 0 || num == 1) {
            return true;
        }

        int left = 0;
        int right = num + 1;
        while (left + 1 != right) {
            int mid = (left + right) / 2;
            if (num / mid == mid && num % mid == 0) {
                return true;
            }
            if (mid < num / mid) {
                left = mid;
            } else {
                right = mid;
            }
        }

        return false;
    }

    public static void main(String[] args) {
        System.out.println(isPerfectSquare(15));
        System.out.println(isPerfectSquare(16));
        System.out.println(isPerfectSquare(17));

    }
}
